//
//  ITBaseTipView.m
//  InTube
//
//  Created by seven on 2019/11/1.
//  Copyright © 2019 seven. All rights reserved.
//

#import "ITBaseTipView.h"

@interface ITBaseTipView(){
    DisQueueTask _hideTask;
}

@end

@implementation ITBaseTipView

+ (instancetype)showWithAction:(CustomAction)action{
    return [self showInView:nil frame:CGRectZero withAction:action];
}

+ (instancetype)showInView:(UIView *)view frame:(CGRect)frame withAction:(CustomAction)action{
    if (view == nil) {
        view = [UIViewController currentTopViewController].view;
    }
    if (CGRectEqualToRect(CGRectZero, frame)) {
        frame = view.bounds;
    }
    ITBaseTipView *tip = [self initWithFrame:frame];
    tip.customAction = action;
    [tip showInView:view];
    return tip;
}

+ (instancetype)viewWithModel:(id _Nullable)model frame:(CGRect)frame action:(CustomAction)action{
    ITBaseTipView * view = [[self alloc] initWithFrame:frame];
    view.customAction = action;
    if (model) view.model = model;
    return view;
}

- (void)initViews{
    [super initViews];
    _animationTime = 0.3;
    //[self hideChanged];
    /*
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (self.customAction) {
            self.customAction(self, cat_tap);
        }
    }];
    [self addGestureRecognizer:tap];
     //*/
}


- (void)setModel:(id)model{
    _model = model;
    [self updateViews];
}

- (void)updateViews{}

- (void)showInView:(UIView *)view{
    [self showInView:view hideAfter:-1];
}

- (void)showInView:(UIView *)view hideAfter:(int)time{
    [view addSubview:self];
    [self hideChanged];
    [self showAnimation];
    [self hideAfter:time];
}

- (void)showChanged{
    self.alpha = 1;
}

- (void)hideChanged{
    self.alpha = 0;
}

- (void)showAnimation{
    //Spring 0-1 越小越明显 initialSpringVelocity则表示初始的速度，数值越大一开始移动越快。
    [UIView animateWithDuration:_animationTime delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:4 options:(UIViewAnimationOptionAllowUserInteraction) animations:^{
        [self showChanged];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hideAnimation{
    [UIView animateWithDuration:_animationTime delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:(UIViewAnimationOptionAllowUserInteraction) animations:^{
        [self hideChanged];;
    } completion:^(BOOL finished) {
        if (!_stayWhenHide) {
            [self removeFromSuperview];
        }
    }];
    if (self.customAction) {
        self.customAction(self, cat_hide);
    }
}

- (void)hideAfter:(int)time{
    if (_hideTask) {
        _hideTask(YES);
    }
    if (time < 0) return;

    _hideTask= MainDelay(time, ^{
        [self hideAnimation];
    });
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
}



- (BOOL)canAutoHide{
    return YES;
}

#pragma mark - fullScreen

- (void)setFullScreen:(BOOL)fullScreen{
    if (_fullScreen == fullScreen) {
        return;
    }
    _fullScreen = fullScreen;
    
    [self updateFullScreen];
}

- (void)updateFullScreen{
    
}

@end
